package moh.s2.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import moh.s2.s2Facade.AsFacade;
import moh.s2.s2entity.AppSettingVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/c5")
public class AsController {

	
	@Autowired
	@Qualifier("asFacade")
	private AsFacade asFacade;
	

	@RequestMapping(method=RequestMethod.GET)
	public String showPage(Model model , HttpServletRequest req, HttpServletResponse resp){
		System.out.println("0............ASController showPage");
		String s="c5";
		String pp  = req.getParameter("p");
		System.out.println("0............ASController showPage " + pp);
		
		if (pp!=null && pp.equals("newas")) {
			s = "newas"; //"c5/newas";
			System.out.println("1............p=newas | ASController showPage");
		} 
		else if (pp!=null && pp.equals("updateAs")) {
			System.out.println("ASController showPage updateas");
			int id = Integer.parseInt(req.getParameter("id"));
			System.out.println("ASController showPage updateas " + id);
			AppSettingVO vo = asFacade.get(id);
			System.out.println("ASController showPage updateas " + id + " " + vo);
			model.addAttribute("as", vo);
			s = "newas"; //"c5/newas";
			System.out.println("2............p=updateAs | ASController showPage");
		}
		else {
			List<AppSettingVO> lst = asFacade.getAppSettings();
		
			for(AppSettingVO v:lst) {
				//System.out.println(v);
			}
			model.addAttribute("aslist", lst);
			System.out.println("3...........else | ASController showPage");
		}
		System.out.println("3...........return S | ASController showPage :"+s);
		return s; //"c5";
	}

	
	@RequestMapping(value="newas", method=RequestMethod.GET)
	public String showNewAsPage(Model model , HttpServletRequest req, HttpServletResponse resp){
		return "c5/newas";
	}

	
	
	
	@RequestMapping(value="search", method=RequestMethod.GET)
	public @ResponseBody List<AppSettingVO> searchAsSettings(Model model , HttpServletRequest req, HttpServletResponse resp){
		String mainid = req.getParameter("mainid");
		System.out.println("AsController.searchAsSettings " + mainid);
		//String sname = req.getParameter("sname");
		return asFacade.getAppSettings(mainid);
	}
	

	@RequestMapping(value="addAs", method=RequestMethod.POST)
	public  String addSetting(Model model, HttpServletRequest req, HttpServletResponse resp){
		//@ResponseBody
		System.out.println("1.................addSetting");
		AppSettingVO vo  = new AppSettingVO();
		vo.setMainId(req.getParameter("mainid"));
		vo.setSubId(req.getParameter("subid"));
		vo.setLanguage(req.getParameter("lang"));
		vo.setParamName(req.getParameter("pname"));
		vo.setValue(req.getParameter("val"));
		vo.setIndex(Integer.parseInt(req.getParameter("idx")));
		
		System.out.println("2.................addSetting  "+ vo.toString());
		asFacade.add(vo);
		
		List<AppSettingVO> lst = asFacade.getAppSettings();
		model.addAttribute("aslist", lst);
		
		System.out.println(req.getRequestURL().toString());

		
		return "redirect:/c5"; //"persfacade.getPersonList()";
	}

	@RequestMapping(value="updateAs", method=RequestMethod.POST)
	public  String updateSetting(Model model, HttpServletRequest req, HttpServletResponse resp){
		//@ResponseBody
		System.out.println("1.................updateSetting " + req.getParameter("mId"));
		
		AppSettingVO vo  =  asFacade.get(Integer.parseInt(req.getParameter("mId"))); // new AppSettingVO();
		
		System.out.println("1.................updateSetting " + vo);
		
		vo.setMainId(req.getParameter("mainid"));
		vo.setSubId(req.getParameter("subid"));
		vo.setLanguage(req.getParameter("lang"));
		vo.setParamName(req.getParameter("pname"));
		//vo.setValue(req.getParameter("val"));
		//vo.setIndex(Integer.parseInt(req.getParameter("idx")));
		
		System.out.println("2.................updateSetting  "+ vo.toString());
		asFacade.update(vo);
		
		List<AppSettingVO> lst = asFacade.getAppSettings();
		model.addAttribute("aslist", lst);
		
		return "/c5"; //"persfacade.getPersonList()";
	}
	

	/*@RequestMapping(value="addperson", method=RequestMethod.POST)
	public @ResponseBody List<Person> addPerson(Model model , HttpServletRequest req, HttpServletResponse resp){
		
		String fname = req.getParameter("fname");
		String sname = req.getParameter("sname");
		
		Person p = new Person();
		p.setFirstName(fname);
		p.setSecondName(sname);
		persfacade.addPerson(p);
		
		return persfacade.getPersonList();
	}*/
	
	


	/*
	@RequestMapping(value="summary", method=RequestMethod.GET)
	public @ResponseBody SummaryDto summary(Model model , HttpServletRequest req, HttpServletResponse resp){
		
		System.out.println("summary");
		
		//DefaultSummaryFacadeImpl faca = new DefaultSummaryFacadeImpl();
		
		SummaryDto dto = sumdtofacade.getSummary();
		
		String fname = req.getParameter("fname");
		String sname = req.getParameter("sname");
		
		
		dto.setName(fname.toUpperCase() + " " + sname.toUpperCase());
		
		dto.setSummary("Hello.... <b>" + fname + "</b>  you are welcome. [sz] "  + persfacade.getPersonList().size());
		
		return dto;
	}
	*/
	
	@RequestMapping(value="page2",method=RequestMethod.GET)
	public String searchPage(Model model , HttpServletRequest req, HttpServletResponse resp){
		return "page2";
	}
	
	
	public AsFacade getAsFacade() {
		return asFacade;
	}

	public void setAsFacade(AsFacade asFacade) {
		this.asFacade = asFacade;
	}

	
	
}
